package com.yxw.yxnet_cd_center.customer_service.service.impl;

import com.yxw.yxnet_cd_center.customer_service.entity.YxCsCustTagEntity;
import com.yxw.yxnet_cd_center.customer_service.entity.YxCsTagEntity;
import com.yxw.yxnet_cd_center.customer_service.mapper.CustMapper;
import com.yxw.yxnet_cd_center.customer_service.mapper.RelevancyMapper;
import com.yxw.yxnet_cd_center.customer_service.service.ICustService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 客户Service业务层处理
 *
 * @author xiexuxin
 * @date 2023-10-20
 */
@Service
public class CustServiceImpl implements ICustService {

    @Autowired
    private CustMapper custMapper;

    @Autowired
    private RelevancyMapper relevancyMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertCustTags(List<YxCsCustTagEntity> custTags) {
        for (YxCsCustTagEntity custTag : custTags) {
            Long custId = custTag.getCustId();
            relevancyMapper.deleteCustTagBycustId(custId);
        }
        if (custTags.stream().anyMatch(custTag -> custTag.getLabelTypes() == null || custTag.getLabelTypes().length == 0)) {
            return;
        }
        relevancyMapper.insertCustTags(custTags);
    }

    @Override
    public List<YxCsTagEntity> getTagsByCustId(Long custId) {
        return custMapper.getTagsByCustId(custId);
    }


}
